// 手写实现myPromise源码:
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'



class MyPromise {
    status = PENDING;
    value = undefined;
    reason = undefined;
    successCallback = [];
    failCallback = [];
    constructor(executor){
        try {
            executor(this.resolve,this.reject)
        }catch (e) {
            this.reject(e)
        }

    }

    resolve = (value) =>{
        if(this.status !== PENDING) return
        this.value = value
        this.status = FULFILLED
        // this.successCallback&&this.successCallback(value)
        while (this.successCallback.length) this.successCallback.shift()()

    }
    reject = (reason) => {
        if(this.status !== PENDING) return
        this.reason = reason
        this.status = REJECTED
        // this.failCallback&&this.failCallback(reason)
        while (this.failCallback.length) this.failCallback.shift()()

    }

    finally (callback){
        return this.then(value=>{
            return MyPromise.resolve(callback()).then(()=>value)
        },reason=>{
            return MyPromise.resolve(callback()).then(()=>{
                throw reason
            })
        })
    }
    catch(failCallback){
        return this.then(undefined,failCallback)
    }
    then(successCallback,failCallback){
        let promise2 = new MyPromise((resolve,reject)=>{
            if(this.status === FULFILLED){

                setTimeout(()=>{
                    try {
                        let x = successCallback(this.value)
                        resolvePromise(promise2,x,resolve,reject)
                    }catch (e) {
                        reject(e)
                    }

                },0)

            }else if(this.status === REJECTED){
                setTimeout(()=>{
                    try {
                        let x = failCallback(this.reason)
                        resolvePromise(promise2,x,resolve,reject)
                    }catch (e) {
                        reject(e)
                    }

                },0)
            }else{
                this.successCallback.push(()=>{
                    setTimeout(()=>{
                        try {
                            let x = successCallback(this.value)
                            resolvePromise(promise2,x,resolve,reject)
                        }catch (e) {
                            reject(e)
                        }

                    },0)
                })
                this.failCallback.push(()=>{
                    setTimeout(()=>{
                        try {
                            let x = failCallback(this.reason)
                            resolvePromise(promise2,x,resolve,reject)
                        }catch (e) {
                            reject(e)
                        }

                    },0)
                })
            }
        })
        return promise2

    }
    static all(array){
        let result = []
        let index = 0

        return new MyPromise((resolve,reject)=>{
            function addData(key,value) {
                result[key] = value
                index++
                if(index === array.length){
                    resolve(result)
                }
            }
            for(let i = 0;i<array.length;i++){
                let current = array[i]
                if(current instanceof MyPromise){
                    current.then(value=>addData(i,value),reason=>reject(reason))
                }else{
                    // 普通值
                    addData(i,array[i])
                }
            }
        })
    }
    static resolve(value){
        if(value instanceof MyPromise){
            return value
        }else {
            return new MyPromise((resolve,reject)=>{
                resolve(value)
            })
        }
    }

}

function resolvePromise(promise,x,resolve,reject){
    if(promise === x){
        return reject(new TypeError('发生了promise循环调用'))
    }
    if(x instanceof MyPromise){
        x.then(resolve,reject)
    }else{
        resolve(x)
    }
}

let p1 = new MyPromise((resolve,reject)=>{
    setTimeout(function () {
        resolve(1)
    },3000)
})

let p2 = new MyPromise((resolve,reject)=>{
    reject('p2 reject')
})
// MyPromise.all(['a','b',p1,p2]).then(function (result){
//     console.log(result)
// })
//
// MyPromise.resolve('p1').then(value => console.log(value))
// MyPromise.resolve(p1).then(value => console.log(value))

// p2.finally(()=>{
//     console.log('finally')
//     return p1
// }).then(value => {console.log(value)},
//     reason => {console.log(reason)})
p2.then(value=>{

}).catch(reason=>{
    console.log(reason)
})